/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.commands;

import dale2507.gates.Messenger;
import dale2507.gates.data.permissions.Permissions;
import dale2507.gates.exceptions.PermissionException;
import dale2507.gates.exceptions.VerificationException;
import dale2507.gates.gate.GateBuilder;
import dale2507.gates.misc.DirectionSensitiveLocation;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 *
 * @author Dale
 */
public class SpawnGate implements CommandExecutor {
    
    private static final HashSet<Byte> ignoreBlocks = new HashSet<>();
    
    public SpawnGate() {
        ignoreBlocks.add((byte) Material.AIR.getId());
        ignoreBlocks.add((byte) Material.SNOW.getId());
        ignoreBlocks.add((byte) Material.WATER.getId());
        ignoreBlocks.add((byte) Material.STATIONARY_WATER.getId());
        ignoreBlocks.add((byte) Material.WHEAT.getId());
        ignoreBlocks.add((byte) Material.DEAD_BUSH.getId());
        ignoreBlocks.add((byte) Material.NETHER_WARTS.getId());
        ignoreBlocks.add((byte) Material.VINE.getId());
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        if (cs instanceof Player) {
            try {
                Player p = (Player) cs;
                this.checkPermissions(p, null);
                
                Block b = p.getTargetBlock(ignoreBlocks, 10);
                if (b != null) {
                    DirectionSensitiveLocation dsl = new DirectionSensitiveLocation(b.getLocation());
                    dsl.setYaw(p.getLocation().getYaw());
                    dsl.setPitch(p.getLocation().getPitch());
                    
                    if (dsl.getPitch() < -60 || dsl.getPitch() > 60) {
                        dsl = dsl.getRelative(0, 0, -1);
                    } else {
                        dsl = dsl.getRelative(0, 4, 0);
                    }
                    
                    try {
                        new GateBuilder().spawnRing(dsl);
                        
                        p.getInventory().addItem(new ItemStack(Material.CHEST, 1), new ItemStack(Material.SIGN, 1));
                        Messenger.sendMessage(cs, Messenger.SUCCESS, "Gate Spawned.");
                        return true;
                    } catch (VerificationException ex) {
                        Messenger.sendMessage(cs, Messenger.ERROR, "Unable to create gate: " + ex.getMessage());
                        Logger.getLogger(SpawnGate.class.getName()).log(Level.SEVERE, null, ex);
                        return true;
                    }
                } else {
                    Messenger.sendMessage(cs, Messenger.ERROR, "No block targetted");
                    return true;
                }
            } catch (PermissionException ex) {
                Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
                return true;
            }
        } else {
            Messenger.sendMessage(cs, Messenger.ERROR, "Not supported");
            return true;
        }
    }

    private void checkPermissions(Player p, String world) throws PermissionException {        
        World w = null;
        if (world != null && !world.isEmpty()) {
            w = Bukkit.getWorld(world);
        } else if (p != null) {
            p.getWorld();
        }
        
        if (!Permissions.getPermissions().getCommandPermissions().allowSpawnGate(p)) {
            throw new PermissionException("You are not allowed to use that command.");
        }
    }
    
}
